package com.hxkj.service.impl;


import com.hxkj.client.service.AuthRoleServiceClient;
import com.hxkj.domain.enums.AuthRoleExceptionEnum;
import com.hxkj.domain.enums.SiteExceptionEnum;
import com.hxkj.domain.po.AllocationFunction;
import com.hxkj.domain.po.AuthRole;
import com.hxkj.service.AuthRoleService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户mapper
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@Service
@Slf4j
public class AuthRoleServiceImpl implements AuthRoleService {


    @Autowired
    private AuthRoleServiceClient authRoleServiceClient;


    @Override
    public ResultMessageStruct saveAuthRole(AuthRole authRole) throws Exception {
        if (authRole == null) {
            throw new CustomException(AuthRoleExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            // 查询站点列表
            result = authRoleServiceClient.saveAuthRole(authRole);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));

        }
    }

    @Override
    public ResultDataStruct<AuthRole> getAuthRole(AuthRole authRole) throws Exception {
        if (authRole == null) {
            throw new CustomException(AuthRoleExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultDataStruct<AuthRole> result = new ResultDataStruct();
        try {
            result = authRoleServiceClient.getAuthRole(authRole);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.AUTHROLE_QUERY_ERROR);
        }
    }

    @Override
    public ResultPageStruct<AuthRole> getRoleList(AuthRole authRole, PageTo pageParam) throws Exception {
        ResultPageStruct<AuthRole> result = new ResultPageStruct<>();
        ParameterDataStruct<AuthRole> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(authRole);
        parameterDataStruct.setPageTo(pageParam);
        try {
            // 查询站点列表
            result = authRoleServiceClient.getRoleList(parameterDataStruct);
            result.setCode(null);
            result.setMessage(null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.AUTHROLE_QUERY_ERROR);

        }

    }

    @Override
    public ResultPageStruct<AuthRole> getAuthRoleBySlave(AuthRole authRole, PageTo pageParam) throws Exception {
        ResultPageStruct<AuthRole> result = new ResultPageStruct<>();
        ParameterDataStruct<AuthRole> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(authRole);
        parameterDataStruct.setPageTo(pageParam);
        try {
            // 查询站点列表
            result = authRoleServiceClient.getUserBySlave(parameterDataStruct);
            result.setCode(null);
            result.setMessage(null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.AUTHROLE_QUERY_ERROR);

        }

    }


    @Override
    public ResultMessageStruct updateAuthRole(AuthRole authRole) throws Exception {
        if (authRole == null) {
            throw new CustomException(AuthRoleExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = authRoleServiceClient.updateAuthRole(authRole);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    @Override
    public ResultMessageStruct deleteAuthRole(AuthRole authRole) throws Exception {
        if (authRole == null) {
            throw new CustomException(AuthRoleExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = authRoleServiceClient.deleteAuthRole(authRole);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
        }
    }


    @Override
    public AllocationFunction getFunction(AuthRole authRole) throws Exception {
        ResultDataStruct<AllocationFunction> result = new ResultDataStruct();
        try {
            // 获取角色分配功能
            result = authRoleServiceClient.getFunction(authRole);
        } catch (Exception e) {
            log.error("获取角色分配功能失败:{}", e.getMessage());
            e.printStackTrace();
            throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
        }
        return result.getData();
    }

    @Override
    public AllocationFunction getFunctions(AuthRole authRole) throws Exception {
        ResultDataStruct<AllocationFunction> result = new ResultDataStruct();
        if (authRole.getSiteId() == null) {
            throw new CustomException(SiteExceptionEnum.SITE_NOT_ID);
        }
        if (authRole.getRoleId() == null) {
            throw new CustomException(AuthRoleExceptionEnum.AUTHROLE_NOT_ID);
        }
        try {
            // 获取角色分配功能
            result = authRoleServiceClient.getFunctions(authRole);
            if (result.getCode() != null) {
                throw new CustomException(AuthRoleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result.getData();
    }

    @Override
    public ResultMessageStruct saveAllocationFunction(AuthRole authRole) throws Exception {
        // 给角色分配功能
        ResultMessageStruct result = authRoleServiceClient.allocationFunction(authRole);
        if (result.getCode() != 200) {
            throw new CustomException(AuthRoleExceptionEnum.ROLE_SET_FUNCTION_ERROR);
        }

        return result;
    }
}
